package com.filemanager;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.*;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class FileOperations {
    public static void copyFile(File source, File dest) throws Exception {
        if (source.isDirectory()) {
            copyDirectory(source, dest);
        } else {
            Files.copy(source.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
        }
    }
    public static void copyDirectory(File source, File target) throws Exception {
        if (!target.exists()) {
            target.mkdirs();
        }
        for (File file : source.listFiles()) {
            File dest = new File(target, file.getName());
            if (file.isDirectory()) {
                copyDirectory(file, dest);
            } else {
                Files.copy(file.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
            }
        }
    }
    public static boolean deleteFileOrDirectory(File file) {
        if (file.isDirectory()) {
            File[] contents = file.listFiles();
            if (contents != null) {
                for (File f : contents) {
                    if (!deleteFileOrDirectory(f)) return false;
                }
            }
        }
        return file.delete();
    }
    public static boolean renameFile(File oldFile, File newFile) {
        return oldFile.renameTo(newFile);
    }
    public static boolean createFolder(File parent, String name) {
        File newFolder = new File(parent, name);
        return newFolder.mkdir();
    }
} 